package com.gyx.metaldesigndemo.recycle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Menu;
import android.view.MenuItem;

import com.gyx.metaldesigndemo.R;

public class RecycleViewActivity extends AppCompatActivity {


    private RecyclerView recyclerView;

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_view);

        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        
        initToolBar();
        
        testLinearLayoutManager();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_recyclview,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu1:
                testLinearLayoutManager();
                break;
            case R.id.menu2:
                testGridLayoutManager();
                break;
            case R.id.menu3:
                testLinearLayoutDragged();
                break;
            case R.id.menu4:
                testGridLayoutDragged();
                break;
            case R.id.menu5:
                testStaggeredGridLayout();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void testStaggeredGridLayout() {
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

        recyclerView.setAdapter(new RecyclerViewStaggeredAdapter(this));
    }

    private void testGridLayoutDragged() {
        RecyclerViewDraggedAdapter adater=new RecyclerViewDraggedAdapter(this);

        SimpleTouchHelperCallBack_Grid callBack=new SimpleTouchHelperCallBack_Grid(adater);

        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callBack);

        itemTouchHelper.attachToRecyclerView(recyclerView);

        recyclerView.setLayoutManager(new GridLayoutManager(this,3));

        recyclerView.setAdapter(adater);
    }

    private void testLinearLayoutDragged() {

        RecyclerViewDraggedAdapter adater=new RecyclerViewDraggedAdapter(this);

        SimpleTouchHelperCallBack callBack=new SimpleTouchHelperCallBack(adater);

        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callBack);

        itemTouchHelper.attachToRecyclerView(recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        recyclerView.setAdapter(adater);


    }

    private void testGridLayoutManager() {

        recyclerView.setLayoutManager(new GridLayoutManager(this,3));

        recyclerView.setAdapter(new RecyclerViewAdapter(this));

    }

    private void testLinearLayoutManager() {

        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        recyclerView.setAdapter(new RecyclerViewAdapter(this));

    }

    private void initToolBar() {

        setSupportActionBar(toolbar);
    }
}
